package com.xiaodou.mjf.web.console.merchant;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.core.manager.OrderManager;
import com.xiaodou.mjf.application.core.repository.mybatis.OrderMapper;
import com.xiaodou.mjf.application.core.repository.mybatis.OrderStatementMapper;
import com.xiaodou.mjf.application.merchant.service.MerchantService;
import com.xiaodou.mjf.entity.core.Order;
import com.xiaodou.mjf.entity.core.OrderStatement;
import com.xiaodou.mjf.entity.merchant.Merchant;
import com.xiaodou.mjf.infrastructure.common.components.Result;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;

/**
 * 商户管理
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("merchant")
public class MerchantController extends ApplicationController {

	@Resource
	MerchantService merchantService;
	@Resource
	OrderMapper orderMapper;
	@Resource
	OrderStatementMapper orderStatementMapper;
	@Resource
	OrderManager orderSettlementManager;

	@RequestMapping("")
	public String merchantList(Merchant merchant) {
		return "merchant/list";
	}

	@RequestMapping("order")
	public String orderList(Merchant merchant) {
		return "merchant/order_list";
	}

	/**
	 * 获取商户列表
	 * 
	 * @param merchant
	 * @return
	 */
	@RequestMapping(value = { "merchantData.json" })
	@ResponseBody
	public Map<String, Object> merchantData(Merchant merchant) {
		Page<Merchant> page = merchantService.page(merchant);

		return page(page);
	}

	/**
	 * 商户信息查看
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "view", "view/{id}" }, method = RequestMethod.GET)
	public String view(@PathVariable String id, Model model) {

		Merchant merchant = merchantService.get(id);
		model.addAttribute("merchant", merchant);
		return "merchant/update";
	}

	/**
	 * 商户信息修改
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = { "update" }, method = RequestMethod.POST)
	public String update(Merchant merchant) throws Exception {
		if (merchant != null)
			merchantService.save(merchant);

		return "merchant/list";
	}

	/**
	 * 商户信息删除
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "delete", "delete/{id}" })
	@ResponseBody
	public Map<String, Object> delete(@PathVariable String id, Model model) {
		Merchant merchant = merchantService.get(id);
		if (merchant == null) {
			return map(false);
		}
		merchant.setStatus(Merchant.DELETE);
		merchantService.save(merchant);
		return map(true);
	}

	@RequestMapping(value = { "form" }, method = RequestMethod.GET)
	public String form(Merchant merchant, Model model) {

		return "merchant/form";
	}

	/**
	 * 订单列表
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = { "orderData.json" })
	@ResponseBody
	public Map<String, Object> orderData(Order order) {
		List<Order> entity = Lists.newArrayList();
		if (order.getStart() == 1) {
			order.setStart(0);
		} else {
			order.setStart(order.getStart() - 1);
		}
		Integer count = orderMapper.getMerchantOrderListCount(order);

		entity = orderMapper.getMerchantOrderList(order);

		return page(count, entity);
	}

	/**
	 * 商户结算列表
	 * 
	 * @param orderStatement
	 * @return
	 */
	@RequestMapping(value = { "settlement" })
	public String settlement(OrderStatement orderStatement) {

		return "merchant/settlement";

	}

	@RequestMapping(value = { "settlementData.json" })
	@ResponseBody
	public Map<String, Object> settlementData(OrderStatement orderStatement) {

		List<OrderStatement> entity = Lists.newArrayList();
		if (orderStatement.getStart() == 1) {
			orderStatement.setStart(0);
		} else {
			orderStatement.setStart(orderStatement.getStart() - 1);
		}
		Integer count = orderStatementMapper.getMerchantSettlementCount(orderStatement);
		entity = orderStatementMapper.getMerchantSettlementList(orderStatement);

		return page(count, entity);

	}

	/**
	 * 商户结算
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = { "orderSettlement" })
	@ResponseBody
	public Map<String, Object> orderSettlement(String orderIds, Double amount) throws Exception {
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtils.getCurrentTime());
		Integer year = cal.get(Calendar.YEAR);
		Integer week = cal.get(Calendar.WEEK_OF_YEAR);

		Result result = orderSettlementManager.orderSettlement(orderIds, amount, year, week, DateUtils.getCurrentTime());

		return map(result.isSuccess(), result.getMessage());
	}

}
